package com.andreamerla.petslife.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import com.andreamerla.petslife.R;
import com.andreamerla.petslife.fragment.VetSearchListFragment;
import com.andreamerla.petslife.fragment.VetSearchMapFragment;
import com.andreamerla.petslife.model.Place;
import com.andreamerla.petslife.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 19/07/13
 * Time: 17.45
 */
public class VetSearchActivity extends GpsActivity {
    private static final String SELECTED_TAB = "selected_tab";
    private static final String LIST_TAB = "list_tab";
    private static final String MAP_TAB = "map_tab";

    private ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Notice that setContentView() is not used, because we use the root android.R.id.content as the container for each fragment

        // Setup action bar for tabs
        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);

        // Add "List" tab
        ActionBar.Tab tab = actionBar.newTab()
                .setText(R.string.list)
                .setTabListener(new TabListener<VetSearchListFragment>(this, LIST_TAB, VetSearchListFragment.class));
        actionBar.addTab(tab);

        // Add "Map" tab
        tab = actionBar.newTab()
                .setText(R.string.map)
                .setTabListener(new TabListener<VetSearchMapFragment>(this, MAP_TAB, VetSearchMapFragment.class));
        actionBar.addTab(tab);

        // Restore saved state
        if (savedInstanceState != null) {
            actionBar.setSelectedNavigationItem(savedInstanceState.getInt(SELECTED_TAB));
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Utils.GOOGLE_PLAY_SERVICES_REQUEST_CODE) {
            // Pass onActivityResult call to map fragment
            VetSearchMapFragment mapFragment = (VetSearchMapFragment) getSupportFragmentManager().findFragmentByTag(MAP_TAB);
            if (mapFragment != null)
                mapFragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save selected tab
        outState.putInt(SELECTED_TAB, actionBar.getSelectedNavigationIndex());
    }

    public List<Place> getPlaceList() {
        VetSearchListFragment fragment = (VetSearchListFragment) getSupportFragmentManager().findFragmentByTag(LIST_TAB);
        return fragment != null ? fragment.getPlaceList() : null;
    }

    public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private Fragment mFragment;
        private final FragmentActivity mActivity;
        private final String mTag;
        private final Class<T> mClass;

        /**
         * Constructor used each time a new tab is created.
         *
         * @param activity The host Activity, used to instantiate the fragment
         * @param tag      The identifier tag for the fragment
         * @param clz      The fragment's Class, used to instantiate the fragment
         */
        public TabListener(FragmentActivity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null)
                mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Usually do nothing.
        }
    }
}
